package com.gupao.study.design.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 注册时单例（类似懒汉），类似缓存模式，和懒加载类似
 * @author asus
 *
 */
public class SingletonRegister {
	private static final Map<String,Object> singletonMap = new ConcurrentHashMap();
	
	static {
		SingletonRegister singletonRegister = new SingletonRegister();
		singletonMap.put(SingletonRegister.class.getName(), singletonRegister);
	}
	
	private SingletonRegister() {
		
	}
	
	public static Object getInstance(String name) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		if(name==null&&"".equals(name)) {
			name = SingletonRegister.class.getName();
		}
		if(singletonMap.get(name)==null) {
			singletonMap.put(name, Class.forName(name).newInstance());
		}
		
		return singletonMap.get(name);
	}
}
